package com.winderinfo.yikaotianxia;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.ui.classification.ClassificationFragment;
import com.winderinfo.yikaotianxia.ui.home.HomeFragment;
import com.winderinfo.yikaotianxia.ui.itembank.ItembankFragment;
import com.winderinfo.yikaotianxia.ui.my.MyFragment;
import com.winderinfo.yikaotianxia.ui.study.StudyFragment;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.main_rg)
    RadioGroup mainRg;
    private HomeFragment homeFragment;
    private ClassificationFragment classificationFragment;
    private ItembankFragment itembankFragment;
    private StudyFragment studyFragment;
    private MyFragment myFragment;
    private FragmentManager manager;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        //获取Fragment
        homeFragment = new HomeFragment();
        classificationFragment = new ClassificationFragment();
        itembankFragment = new ItembankFragment();
        studyFragment = new StudyFragment();
        myFragment = new MyFragment();

        //Fragment管理器
        manager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        fragmentTransaction.add(R.id.main_frame, homeFragment);
        fragmentTransaction.add(R.id.main_frame, classificationFragment);
        fragmentTransaction.add(R.id.main_frame, itembankFragment);
        fragmentTransaction.add(R.id.main_frame, studyFragment);
        fragmentTransaction.add(R.id.main_frame, myFragment);
        //默认显示第一个
        fragmentTransaction.show(homeFragment).hide(classificationFragment).hide(itembankFragment).hide(studyFragment).hide(myFragment);
        //提交
        fragmentTransaction.commit();

        //RadioGroup切换页面
        mainRg.setOnCheckedChangeListener(this);

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        switch (checkedId) {
            case R.id.rb_home:
                fragmentTransaction.show(homeFragment).hide(classificationFragment).hide(itembankFragment).hide(studyFragment).hide(myFragment);
                break;
            case R.id.rb_classification:
                fragmentTransaction.show(classificationFragment).hide(homeFragment).hide(itembankFragment).hide(studyFragment).hide(myFragment);
                break;
            case R.id.rb_itembank:
                fragmentTransaction.show(itembankFragment).hide(classificationFragment).hide(homeFragment).hide(studyFragment).hide(myFragment);
                break;
            case R.id.rb_study:
                fragmentTransaction.show(studyFragment).hide(classificationFragment).hide(itembankFragment).hide(homeFragment).hide(myFragment);
                break;
            case R.id.rb_my:
                fragmentTransaction.show(myFragment).hide(classificationFragment).hide(itembankFragment).hide(studyFragment).hide(homeFragment);
                break;
        }
        //提交
        fragmentTransaction.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
